<template>
    <div class="main set-level-list">
        <div class="filter-container">
            <el-input @keyup.enter.native="search" class="filter-item" placeholder="按名称搜索" v-model="params.activityName"></el-input>
            <el-button class="filter-item" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
            <el-button v-if="rightsButtons['add']" type="success" icon="el-icon-plus" class="filter-item" @click="showAdd">新建</el-button>
        </div>
        <el-table :data="list" v-loading="listLoading" element-loading-text="给我一点时间" border highlight-current-row>
            <el-table-column align="center" label="活动名称" prop="activityName"></el-table-column>
            <el-table-column align="center" label="升级等级" width="100">
                <template slot-scope="scope">
                    <span v-text="levelList[scope.row.upgradeType]"></span>
                </template>
            </el-table-column>
            <el-table-column align="center" label="招募数量" prop="joinInNum" width="100"></el-table-column>
            <el-table-column align="center" label="起始时间">
                <template slot-scope="scope">
                    <span v-text="formatDate(scope.row.startDate, 'YYYY-MM-DD HH:mm:ss')"></span>
                </template>
            </el-table-column>
            <el-table-column align="center" label="结束时间">
                <template slot-scope="scope">
                    <span v-text="formatDate(scope.row.endDate, 'YYYY-MM-DD HH:mm:ss')"></span>
                </template>
            </el-table-column>
            <el-table-column align="center" label="是否启用" width="80">
                <template slot-scope="scope">
                    <span>{{scope.row.isFaceToAllUser == 1 ? '启用' : '不启用'}}</span>
                </template>
            </el-table-column>
            <el-table-column align="center" label="创建时间">
                <template slot-scope="scope">
                    <span v-text="formatDate(scope.row.createdAt, 'YYYY-MM-DD HH:mm:ss')"></span>
                </template>
            </el-table-column>
            <el-table-column align="center" label="操作" width="160" v-if="rightsButtons['edit'] || rightsButtons['delete']">
                <template slot-scope="scope">
                    <el-button v-if="rightsButtons['edit']" type="primary" size="mini" @click="showEdit(scope.row)">编辑</el-button>
                    <el-button v-if="rightsButtons['delete']" type="danger" size="mini" @click="del(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="pagination-container" v-if="total > 0">
            <el-pagination @current-change="handleCurrentChange" :current-page.sync="page" :page-size="pageSize" :total="total" layout="total, prev, pager, next, jumper"></el-pagination>
        </div>
        <el-dialog :title="editItem.id ? '编辑升级配置' : '新建升级配置'" :visible.sync="showEditPop">
            <el-form :model="editItem" :rules="editRule" size="medium" ref="editItem" label-width="120px">
                <el-form-item label="活动名称:" prop="activityName">
                    <el-input v-model="editItem.activityName"></el-input>
                </el-form-item>
                <el-form-item label="升级等级:" prop="upgradeType" :required="true">
                    <el-select v-model="editItem.upgradeType">
                        <el-option v-for="(name, val) in levelList" :key="val" :label="name" :value="val"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="招募数量:" prop="joinInNum">
                    <el-input v-model="editItem.joinInNum"></el-input>
                </el-form-item>
                <el-form-item label="限制时间:">
                    <el-date-picker
                        v-model="timeRange"
                        type="datetimerange"
                        range-separator="~"
                        start-placeholder="开始时间"
                        end-placeholder="结束时间">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="是否启用:">
                    <el-switch
                        v-model="editItem.isFaceToAllUser"
                        active-color="#13ce66"
                        inactive-color="#ff4949"
                        :active-value="1"
                        :inactive-value="0">
                    </el-switch>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="save" :disabled="btnDisabled">保存</el-button>
                <el-button @click="showEditPop = false">取消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>

    export default {
        name: 'setLevelList',
        data() {
            return {
                list: [],
                params: {
                    activityName: ''
                },
                query: {},
                page: 1,
                pageSize: 20,
                total: 0,
                levelList: {
                    '1': '董事',
                    '2': '大区',
                    '3': '兰蜜',
                    '4': '普通会员',
                    '5': '分红董事',
                },
                editItem: {},
                editName: '',
                editRule: {
                    activityName: [
                        {required: true, message: '不能为空', trigger: 'change'},
                        {max: 100, message: '不能超过100字符', trigger: 'change'},
                        {pattern: /[\a-zA-Z0-9\u4e00-\u9FA5]+$/, message: '不能含有特殊字符', trigger: 'change'},
                    ],
                    joinInNum: [
                        {required: true, message: '不能为空', trigger: 'change'},
                        {pattern: /^[1-9]\d*$/, message: '必须为正整数', trigger: 'change'},
                    ]
                },
                timeRange: [],
                showEditPop: false,
                listLoading: false,
                btnDisabled: false,
            }
        },
        created() {
            this.getList();
        },
        methods: {
            // 获取列表
            getList() {
                this.listLoading = true;
                this.request({
                    url: '/bsnl-manage/manage/update/setting/query',
                    method: 'post',
                    data: {
                        pageIndex: this.page,
                        pageSize: this.pageSize,
                        ...this.query
                    }
                }).then(({data, totalCount}) => {
                    this.list = data;
                    this.total = totalCount;
                    this.listLoading = false;
                }).catch(err => {
                    console.log(err);
                    this.list = [];
                    this.total = 0;
                    this.listLoading = false;
                });
            },
            // 删除
            del(item) {
                this.$confirm(`确定要删除 ${item.activityName} 吗?`, '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.request({
                        url: '/bsnl-manage/manage/update/setting/delete/flag',
                        method: 'post',
                        data: {
                            id: item.id,
                            deleteFlag: 0
                        }
                    }).then(() => {
                        if (this.page > 1 && this.list.length === 1) {
                            this.page--;
                        }
                        this.getList();
                        this.$message.success('删除成功');
                    }).catch(err => {
                        console.log(err);
                        this.list = [];
                        this.total = 0;
                        this.listLoading = false;
                    });
                }).catch(err => {
                    console.log(err);
                });
            },
            // 显示新建
            showAdd() {
                this.editItem = {
                    activityName: '',
                    upgradeType: '3',
                    joinInNum: '',
                    isFaceToAllUser: 1
                };
                this.timeRange = [];
                this.showEditPop = true;
                this.btnDisabled = false;
            },
            // 显示编辑
            showEdit(item) {
                this.editItem = {...item};
                this.editItem.upgradeType = this.editItem.upgradeType + '';
                this.editName = item.name;
                this.timeRange = [item.startDate, item.endDate];
                this.showEditPop = true;
                this.btnDisabled = false;
            },
            // 保存
            save() {
                this.$refs.editItem.validate(valid => {
                    if (!valid) {
                        return false;
                    }
                    let url = this.editItem.id ? '/bsnl-manage/manage/update/setting/edit' : '/bsnl-manage/manage/update/setting/add';
                    this.btnDisabled = true;
                    let [start, end] = ['', ''];
                    if (this.timeRange && this.timeRange.length > 0) {
                        start = this.formatDate(this.timeRange[0], 'YYYY-MM-DD HH:mm:ss');
                        end = this.formatDate(this.timeRange[1], 'YYYY-MM-DD HH:mm:ss');
                    }
                    this.request({
                        url,
                        method: 'post',
                        data: {
                            ...this.editItem,
                            startDate: start,
                            endDate: end
                        }
                    }).then(() => {
                        this.btnDisabled = false;
                        this.editItem = {};
                        this.showEditPop = false;
                        this.$message.success('保存成功');
                        this.getList();
                    }).catch(err => {
                        this.btnDisabled = false;
                    });
                });
            },
            // 搜索
            search() {
                this.page = 1;
                this.query = {
                    ...this.params
                };
                this.getList();
            },
            // 切换分页
            handleCurrentChange(page) {
                this.page = page;
                this.getList();
            },
        }
    }
</script>
